/*******************************************************************************
 * Copyright (c) 2008 Smart Mai(smardio@gmail.com).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Smart Mai(smardio@gmail.com) - initial API and implementation
 ******************************************************************************/
package com.smartdio.company.model;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

/**
 * @author smart
 * 
 */
public class CompanyDataProvider implements ITreeContentProvider {

	private Company[] roots = null;

	/**
	 * 
	 */
	public CompanyDataProvider() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.
	 * Object)
	 */
	@Override
	public Object[] getChildren(Object parentElement) {
		if (parentElement instanceof Company) {
			Company company = (Company) parentElement;
			return company.getDepartments().toArray();
		} else if (parentElement instanceof Department) {
			Department departs = (Department) parentElement;
			System.out.println("getChildren Department");
			return departs.getDepartments().toArray();
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object
	 * )
	 */
	@Override
	public Object getParent(Object element) {
		if (element instanceof Company) {
			return null;
		} else if (element instanceof Department) {
			return ((Department) element).company;
		} else if (element instanceof Person) {
			return ((Person) element).department;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.
	 * Object)
	 */
	@Override
	public boolean hasChildren(Object element) {
		if(element instanceof Company){
			return ((Company)element).getDepartments().size()>0;
		}else if(element instanceof Department){
			return ((Department)element).getDepartments().size()>0;
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java
	 * .lang.Object)
	 */
	@Override
	public Object[] getElements(Object inputElement) {
		return roots;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
	 */
	@Override
	public void dispose() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface
	 * .viewers.Viewer, java.lang.Object, java.lang.Object)
	 */
	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		// 将来扩展用于数据库连接
		if (newInput.equals(oldInput))
			return;
		createDemoData();

	}

	/**
	 * 这个方法建立演示的数据
	 */
	private void createDemoData() {
		// 先建立一个根节点，这个根节点关联一个Company对象
		Company company = new Company(true);
		for (int i = 0; i < 10; i++) {
			Department dep = new Department(company, true);
			dep.name = "Department " + (i + 1);
			for(int j=0;j<10;j++){
				Department dep2=new Department(dep, true);
				dep2.name = "Department22" + (j + 1);
			}
		}

		roots = new Company[] { company };
	}

}
